package com.haitao.service.impl;

import com.haitao.common.pojo.EUTreeNode;
import com.haitao.po.ItemCatPo;
import com.haitao.service.ItemCatService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @description: 类描述
 * @author: Fesine
 * @createTime:2017/5/14 21:18
 * @update:修改内容
 * @author: Fesine
 * @updateTime:2017/5/14 21:18
 */
@Service
public class ItemCatServiceImpl extends BaseServiceImpl implements ItemCatService {
    @Override
    public List<EUTreeNode> getCatList(long parentId) {
        List<EUTreeNode> list = new ArrayList<>();
        ItemCatPo itemCatPo = new ItemCatPo();
        itemCatPo.setParentId(parentId);
        List<ItemCatPo> itemCatPoList = daoService.selectList(itemCatPo);
        for (ItemCatPo catPo : itemCatPoList) {
            EUTreeNode node = new EUTreeNode();
            node.setId(catPo.getId());
            node.setText(catPo.getName());
            node.setState(catPo.getIsParent() ? "closed" : "open");
            list.add(node);
        }
        return list;
    }

    private List<EUTreeNode> getItemCatList(long parentId){
        List<EUTreeNode> list = new ArrayList<>();
        ItemCatPo itemCatPo = new ItemCatPo();
        itemCatPo.setParentId(parentId);
        List<ItemCatPo> itemCatPoList = daoService.selectList(itemCatPo);
        for (ItemCatPo catPo : itemCatPoList) {
            EUTreeNode treeNode = new EUTreeNode();
            treeNode.setId(catPo.getId());
            treeNode.setText(catPo.getName());
            if (!catPo.getIsParent()) {
                treeNode.setState("closed");
            } else {
                treeNode.setState("open");
                getItemCatList(catPo.getId());
            }
            list.add(treeNode);
        }
        return list;
    }
}
